package com.wrx.club_management_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wrx.club_management_backend.common.AjaxResult;
import com.wrx.club_management_backend.dto.UserDTO;
import com.wrx.club_management_backend.entity.Comment;
import com.wrx.club_management_backend.entity.News;
import com.wrx.club_management_backend.mapper.NewsMapper;
import com.wrx.club_management_backend.service.CommentService;
import com.wrx.club_management_backend.service.NewsService;
import com.wrx.club_management_backend.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.wrx.club_management_backend.utils.RedisConstant.*;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    NewsMapper newsMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    CommentService commentService;

    @Override
    public AjaxResult getByCurUser() {
        Long userId = UserHolder.getUser().getId();
        //1. 先获取我收藏的社团的id
        Set<String> ids = stringRedisTemplate.opsForSet().members(USER_CLUB_COLLECTION_KEY + userId);
        //2. 获取这些社团的部长
        List<Long> userIds = newsMapper.selectManagerByCLubId(ids);

        //3. 获取朋友的id
        List<Long> friendIds = stringRedisTemplate.opsForSet()
                .members(FRIENDS_KEY + userId) // 获取所有的朋友ids
                .stream().map(Long::parseLong) // 类型转换
                .collect(Collectors.toList()); // 转为list
        //4. 合并两个列表并去重
        List<Long> mergedIds = Stream.concat(userIds.stream(), friendIds.stream()).distinct() // 去除重复元素
                .collect(Collectors.toList());
        mergedIds.add(userId); // 当前用户也要加进去
        //5. 查询这些用户的动态信息
        List<News> news = newsMapper.selectList(new LambdaQueryWrapper<News>().in(News::getUserId, mergedIds));
        return AjaxResult.success(news);
    }

    /**
     * 发表动态
     *
     * @param news
     * @return
     */
    @Override
    public AjaxResult push(News news) {
        UserDTO user = UserHolder.getUser();
        Long userId = user.getId();
        String avatar = user.getAvatar();
        news.setUserId(userId);
        news.setAvatar(avatar);
        this.save(news);
        return AjaxResult.success("发表成功");
    }

    @Override
    public AjaxResult num() {
        Long userId = UserHolder.getUser().getId();
        int size = this.list(
                new LambdaQueryWrapper<News>()
                        .eq(News::getUserId, userId)
        ).size();
        return AjaxResult.success(size);
    }

    @Override
    public AjaxResult like(Long newsId) {
        Long userId = UserHolder.getUser().getId();
        String key = NEWS_LIKE + userId;
        if (this.isLike(newsId)) {
            // 已点赞，取消点赞
            stringRedisTemplate.opsForSet().remove(key, newsId.toString());
            return AjaxResult.success("取消点赞成功");
        } else {
            // 未点赞，添加点赞
            stringRedisTemplate.opsForSet().add(key, newsId.toString());
            return AjaxResult.success("点赞成功");
        }
    }

    @Override
    public boolean isLike(Long newsId) {
        Long userId = UserHolder.getUser().getId();
        String key = NEWS_LIKE + userId;
        // 判断是否已经点赞
       return Boolean.TRUE.equals(stringRedisTemplate.opsForSet().isMember(key, newsId.toString()));
    }
}
